<?php

include_once './_rsa.inc';

function unblind($banksign, $r, $n){

	$ri = gmp_strval(gmp_invert($r,$n)); // r^(-1)
	
	$unblinded = bcmod(bcmul($banksign,$ri),$n);
	
	return $unblinded;
}

function verify_signature($signedMO, $e, $n){

	$num = rsa_encrypt($signedMO, $e, $n);
	$h = dec_to_hex($num);
	
	return $h;
}

function mo_obj_to_string($mo){

$id_hash_s = '';
for($i = 0; $i < sizeof($mo['id_hash']); $i++){
	$id_hash_s .= $mo['id_hash'][$i][0] . ":" . $mo['id_hash'][$i][1];
	if($i != (sizeof($mo['id_hash']) - 1))
		$id_hash_s .= ":";	
}
$mo_string = $mo['uniquestring'] . ":" . $mo['amount'] . ":" . $id_hash_s;

return $mo_string;

}

function is_valid_signature($mo_string, $signature){
	if (strcmp(md5($mo_string),verify_signature($signature, PUBLIC_EXP, MODULUS))==0) return TRUE;
	else return FALSE;
}


function mo_string_to_obj($mo_string){
	
	$mo_str_arr = explode(":", $mo_string);

	$mo['uniquestring'] = $mo_str_arr[0];
	$mo['amount'] = $mo_str_arr[1];
	
	for($i = 2; $i < sizeof($mo_str_arr); $i++)
		$mo['id_hash'][($i - 2)/2][($i % 2)] = $mo_str_arr[$i];

	return $mo;
}

/*
 * Function to create a money order string from database
 */
function MO_from_database($amount, $uniqeStr, $conn){
	$mo['uniquestring'] = $uniqeStr;
	$mo['amount'] = $amount;
	
	$sql = "select idstring_n as n, l_id_string as l, r_id_string as r from mo_idstrings_committed where
		uniqueness_string = '" . $uniqeStr . "' order by n";


	$q_results = db_do_query($conn, $sql);


	$i = 0;
	foreach($q_results as $qr) {
		$id_hash[$i][0] = $qr[L];
		$id_hash[$i][1] = $qr[R];
		$i++;
		}
	
	$mo['id_hash'] = $id_hash;
 	return $mo;
}

function blind($h, $e, $n, $r){
	
	$numstring = hex_to_dec($h);

	// The actual calculations must be done in this order, or PHP will time out calculating 

	$re = bcpowmod($r,$e,$n); // r^e
	$m2 = bcmul($numstring,$re);
	$blind = bcmod($m2,$n);
	
	return $blind;
}
function split_ascii($string){
	
	for($i = 0; $i < strlen($string)/48; $i++){
		$mblock = substr($string, $i*48, 48);
		$str_set[$i] = $mblock;
	}
	
	return $str_set;	
	
}


function ascii_to_num($string){

		for($i = 0; $i < strlen($string); $i++){
			
			$m_int_array[$i] = ord($string[$i]);	

			if($m_int_array[$i] < 10){
				$m_int_array[$i] = (string)"00" . $m_int_array[$i];
				continue;
			}
				if($m_int_array[$i] < 100)
					$m_int_array[$i] = (string)"0" . $m_int_array[$i];
		}

	$num = implode($m_int_array);
	
	return $num;
}

function num_to_ascii($numstring){
	
		while(strlen($numstring) % 3 != 0)
			$numstring = (string)"0" . $numstring;
		
		for($i = 0; $i < (strlen($numstring) / 3); $i++)
			$m_array[$i] = chr($numstring[$i*3] . $numstring[$i*3+1] . $numstring[$i*3+2]);
		
		return implode($m_array);	
	
}

function hex_to_dec($h){
	
	$decvalues = array('0' => '0', '1' => '1', '2' => '2',
    	'3' => '3', '4' => '4', '5' => '5',
        '6' => '6', '7' => '7', '8' => '8',
        '9' => '9', 'a' => '10', 'b' => '11',
        'c' => '12', 'd' => '13', 'e' => '14',
        'f' => '15');
		
	$dec = '0';
    $hrev = strrev($h);
    
    for($i = 0; $i < strlen($hrev); $i++)
    	$dec = bcadd(bcmul(bcpow('16',$i), $decvalues[$hrev[$i]]), $dec);
	
	return $dec;
    
}

function dec_to_hex($d){
	
	$hexvalues = array('0','1','2','3','4','5','6','7',
    	'8','9','a','b','c','d','e','f');
	
	$h = '';
     while($d != '0'){
        $h = $hexvalues[bcmod($d,'16')] . $h;
        $d = bcdiv($d,'16',0);
    }
    while(strlen($h) < 32)
    	$h = "0" . $h;
    
	return $h;
}

?>